//
//  visitorView.swift
//  微博
//
//  Created by 王朝阳 on 16/4/7.
//  Copyright © 2016年 王朝阳. All rights reserved.
//

import UIKit

class visitorView: UIView {
    
    
    // MARK: - 设置视图信息
    /// 设置视图信息
    ///
    /// - parameter imageName: 图片名称，首页设置为 nil
    /// - parameter title:     消息文字
    func setupInfo(imageName: String?, title: String)
    {
        messageLabel.text = title
         // 如果图片名称为 nil，说明是首页，直接返回
        guard let image = imageName else
        {
            //播放首页圆圈转动的动画
            startAnimation()
            return
        }
        //其他页面
        //隐藏小房子
        homeIconView.hidden = true
        //圆圈更换成其他图片
        iconView.image = UIImage(named: image)
        //遮罩移至底层
        sendSubviewToBack(maskIconView)
    }
     /// 开启首页转轮动画
    private func startAnimation()
    {
        //核心动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI //动画的效果
        animation.repeatCount = MAXFLOAT //动画持续的时间
        animation.duration = 60 //多长的时间变化animation.toValue
        
        // 用在不断重复的动画上，当动画绑定的图层对应的视图被销毁，动画会自动被销毁
        animation.removedOnCompletion = false
        
        // 添加到图层
        iconView.layer.addAnimation(animation, forKey: nil)
    }
    //视图还未显示出来时，添加子控件
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加子控件
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    // MARK: - 懒加载控件
    //圆圈图片
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩图片
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //小房子
    private lazy var homeIconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //文字lable
    private lazy var messageLabel: UILabel = UILabel(textstring: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
    //注册按钮
     lazy var registerButton: UIButton = UIButton(title: "注册",color:UIColor.orangeColor(), backgroundImage: "common_button_white_disable")
    
    //登陆按钮
     lazy var loginButton: UIButton = UIButton(title: "登陆",color:UIColor.darkGrayColor(), backgroundImage: "common_button_white_disable")
    
}
extension visitorView
{
    func setupUI()
    {
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2. 设置自动布局
        /**
        - 添加约束需要添加到父视图上
        - 建议，子视图最好有一个统一的参照物！
        */
        // translatesAutoresizingMaskIntoConstraints 默认是 true，支持使用 setFrame 的方式设置控件位置
        // false 支持使用自动布局来设置控件位置
        for v in subviews
        {
            //使控件支持自动布局
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //view1.attr1 = view2.attr2 * multiplier + constant
        //        NSLayoutConstraint(item: 视图,
        //            attribute: 约束属性,
        //            relatedBy: 约束关系,
        //            toItem: 参照视图,
        //            attribute: 参照属性,
        //            multiplier: 乘积,
        //            constant: 约束数值)
        //圆圈的约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60.0))
        
        //小房子的约束
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //文字信息
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16.0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36.0))
        
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16.0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100.0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36.0))
        
        //登陆按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16.0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100.0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: 36.0))
        
        // 6. 遮罩图像
        /**
        VFL : 可视化格式语言
        
        H 水平方向
        V 垂直方向
        | 边界
        [] 包装控件
        views: 是一个字典 [名字: 控件名] - VFL 字符串中表示控件的字符串
        metrics: 是一个字典 [名字: NSNumber] - VFL 字符串中表示某一个数值
        */
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask":maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[regButton]", options: [], metrics: ["btnHeight": -34], views: ["mask": maskIconView, "regButton": registerButton]))
        
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
    }
    
    
}
